#include "SceneNode.h"
#include "Node.h"
#include "Scene.h"

using namespace gull::core;

SceneNode::SceneNode(const char* name, renderPass rp):
ISceneNode(name, rp)
{
	TransformController* tr = new TransformController(this);
    m_controllers[TRANSFORM] = tr;
}

SceneNode::SceneNode(const SceneNode& node):
ISceneNode(node.getName(), node.getRenderPassType())
{
	m_controllers = node.m_controllers;
}

SceneNode::~SceneNode()
{
    
}


gullNotification
SceneNode::update(Scene* scene) 
{
    return GULL_OK;
}

gullNotification 
SceneNode::preRender(Scene* scene) 
{
	TransformController* tr = static_cast<TransformController*>(m_controllers[TRANSFORM]);
    scene->pushAndSetMatrix(*(tr->getTransform()));
    return GULL_OK;
}

gullNotification 
SceneNode::render(Scene* scene) 
{
	const children* allChildren = getChildren();
	int size = allChildren->size();
	
	for (int i = 0; i < size; i++) {
		static_cast<SceneNode*>((*allChildren)[i])->preRender(scene);
		static_cast<SceneNode*>((*allChildren)[i])->render(scene);
		static_cast<SceneNode*>((*allChildren)[i])->postRender(scene);
	}
	
    return GULL_OK;
}

gullNotification 
SceneNode::postRender(Scene* scene) 
{
    scene->popAndSetMatrix();
    return GULL_OK;
}

void
SceneNode::setController(unsigned short ctrl, NodeController* nodeCtrl)
{
	controllersMap::iterator it = m_controllers.find(ctrl) ;
	if(it != m_controllers.end())
	{
		delete (it->second);
		m_controllers.erase(it);
	}
	
	m_controllers.insert(controllerPair(ctrl,nodeCtrl));
}